
#include <cmath>

#include <QFuture>
#include <QList>
#include <QDebug>
#include <QtConcurrentMap>
#include <QtCore/QCoreApplication>

typedef struct {
  double left;
  double right;
} range_type;

double map(const range_type &stride) {
  return sin((stride.right + stride.left) / 2) * (stride.right - stride.left);
}

void reduce(double &res, double x) {
  res += x;
}

int main(int ARGC, char *ARGV[]) {

  QCoreApplication app(ARGC, ARGV);

  const double stride = 1e-5;
  QList<range_type> intervals;

  for (double i = 0; i < M_PI; i += stride) {
    range_type r = { i, i + stride };
    intervals.append(r);
  }

  QFuture<double> result = 
    QtConcurrent::mappedReduced(
      intervals.begin(), 
      intervals.end(), 
      map, 
      reduce
    );

  result.waitForFinished();

  qDebug() << *result.begin();

  return 0;

}
